package cn.niit.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @GetMapping("/login")
    public String loginIndex(){

        return "login";
    }

    @PostMapping("/login")
    public String login(
            Model model,
            HttpSession session,
            @RequestParam("userCode") String userCode, @RequestParam("password")String password) {
        /** 校验参数是否为空 */
        if ("".equals(userCode.trim()) || "".equals(password.trim())) {
            model.addAttribute("errMsg", "用户名/密码 不能为空！");
            System.out.println("用户名/密码 不能为空！");
            return "login";
        }
        System.out.println("用户输入的账号、密码： usercode = " + userCode + " password = " + password);

        Subject loginUser = SecurityUtils.getSubject();

        /** 5、身份认证 */
        try {//生成 try{} catch(){...} 快捷键：ctrl + alt + t
            UsernamePasswordToken token = new UsernamePasswordToken(userCode, password);
            loginUser.login(token);
            System.out.println("用户登录成功");
            /** 用户权限检查 */
        /*    if(loginUser.hasRole("admin")){
                System.out.println("账号" + username + " 拥有 admin 角色！");
            }else{
                System.out.println("【error】账号" + username + " 不是管理员！");
            }
            if(loginUser.isPermitted("teacher")){
                System.out.println("账号" + username + " 拥有 teacher角色！");
            }else{
                System.out.println("【error】账号" + username + " 不是教师！");
            }
            if(loginUser.isPermitted("student")){
                System.out.println("账号" + username + " 拥有 student 角色！");
            }else{
                System.out.println("【error】账号" + username + " 不是学生！");
            }*/


            return "redirect:/";   //走重定向
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("认证失败，失败原因：账号不存在!");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("认证失败，失败原因：密码错误!");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.out.println("认证失败!");
        }
        model.addAttribute("errMsg", "登录失败，账号密码错误！");
        return "login";
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public String logout(){
        Subject loginUser = SecurityUtils.getSubject();
        loginUser.logout();
        return "login";
    }
}
